let requestUrl = null;
let form = new FormData();

function addHandler() {
	requestUrl = "/gang_member/insert";

	$("#member_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#member_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/gang_member/updateById";

		$("#id").textbox("setValue", rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#grade").numberspinner("setValue", rowData.grade);
		$("#schoolId").combobox("setValue", rowData.schoolId);
		$("#jobId").combobox("setValue", rowData.jobId);
		$("#gangId").combobox("setValue", rowData.gangId);
		$("#xybg").numberspinner("setValue", rowData.xybg);
		$("#lsbg").numberspinner("setValue", rowData.lsbg);
		$("#bzbg").numberspinner("setValue", rowData.bzbg);
		$("#enterTime").datebox("setValue", rowData.enterTime);
		$("#lastOnline").datebox("setValue", rowData.lastOnline);

		$("#member_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function importHandler() {
	requestUrl = "/gang_member/import";

	$("#file-name").empty();
	$("#file-size").empty();

	$("#import_dialog").dialog("open");
}

function exportHandler() {
	location.href = "/gang_member/exportData";
}

function deleteHandler() {
	let rowData = $("#member_list").datagrid("getSelected");
	
	if (rowData) {
		$.messager.confirm("系统提示", "是否删除该帮派成员？", function(bool) {
			if (bool) {
				ajaxPost("/gang_member/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#member_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#id").textbox({
		width: 100,
		required: true
	});
	
	$("#name").textbox({
		width: 100,
		required: true
	});
	
	$("#schoolId").combobox({
        url: "/school/selectAll",
		valueField: "id",
		textField: "name",
		width: 100,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		},
		onSelect: function(record) {
	        if(record) {
	        	$("#roleId").combobox("reload", {
					phyle: record.phyle
				});
	        }
	    }
	});
	
	$("#roleId").combobox({
        url: "/role_category/selectAll",
        valueField: "id",
		textField: "name",
		width: 100,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.id
			}, function(resp) {
				let data = resp.data;

				$("#modeling_" + row.id).attr("src", data.image);
			}, error);
			
			return "<img width='20' id='modeling_" + row.id + "' />&nbsp;" + row.name;
		}
    });
	
	$("#grade").numberspinner({
        min: 0,
        max: 115,
		width: 100,
		editable: true,
        required: true
    });
	
	$("#jobId").combobox({
        url: "/gang_job/selectAll",
        valueField: "id",
		textField: "name",
		width: 100,
		required: true,
		panelHeight: "auto"
	});
	
    $("#bzbg").numberspinner({
        min: 0,
        width: 100,
        required: true
    });
    
    $("#xybg").numberspinner({
        min: 0,
        width: 100,
        required: true
    });
    
    $("#lsbg").numberspinner({
        min: 0,
        width: 100,
        required: true
    });

	$("#gangId").combobox({
		url: "/gang/selectAll",
		valueField: "id",
		textField: "name",
		width: 100,
		required: true,
		panelHeight: "auto"
	});
    
    $("#enterTime").datebox({
    	width: 185,
        required: true
    });
    
    $("#lastOnline").datebox({
    	width: 185,
        required: true
    });
    
	$("#member_dialog").dialog({
		title: "成员信息",
		width: 600,
		toolbar: [{
			iconCls: "icon-upload",
			text: "从角色列表导入",
			handler: function() {
				$("#role_dialog").dialog("open");
			}
		}],
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#member_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#member_dialog").dialog("close");
						$("#member_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#member_dialog").dialog("close");
				$("#member_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 200,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_" + row.id).attr("src", data.image);
			}, error);

			return "<img width='18' height='18' id='role_" + row.id + "' />&nbsp;" + row.name;
		},
		onSelect: function(record) {
			if (record) {
				$("#role_name").html(record.name);
				$("#role_grade").html(record.grade);

				ajaxGet("/school/selectById", {
					id: record.schoolId
				}, function (resp) {
					let data = resp.data;

					$("#role_school").html(data.name);
				}, error);

				ajaxGet("/role_category/selectById", {
					id: record.categoryId
				}, function (resp) {
					let data = resp.data;

					$("#role_modeling").html(data.name);
				}, error);
			}
		}
	});

	$("#role_dialog").dialog({
		title: "导入角色",
		width: 300,
		buttons: [{
			iconCls: "icon-ok",
			text: "导入",
			handler: function() {
				let roleId = $("#role_id").combobox("getValue");

				ajaxGet("/role_account/selectById", {
					id: roleId
				}, function (resp) {
					let data = resp.data;

					$("#id").textbox("setValue", data.id);
					$("#name").textbox("setValue", data.name);
					$("#grade").numberspinner("setValue", data.grade);
					$("#roleId").combobox("setValue", data.categoryId);
					$("#schoolId").combobox("setValue", data.schoolId);
				}, error);

				$("#role_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	$("#select_file").filebox({
		buttonText: "选择文件",
		width: 200,
		required: true,
		onChange: function() {
			let file = $(this).context.ownerDocument.activeElement.files[0];

			form.append("file", file);

			$("#file-name").html(file.name);
			$("#file-size").html((file.size / 1024).toFixed(1) + "KB");
		}
	})

	$("#import_dialog").dialog({
		title: "数据导入",
		buttons: [{
			iconCls: "icon-ok",
			text: "导入",
			handler: function() {
				let bool = $("#import_form").form("validate");

				if (bool) {
					postForm(requestUrl, form, function (resp) {
						showMsg(resp.message);

						$("#import_dialog").dialog("close");
						$("#member_list").datagrid("reload");
					}, error);
				} else {
					alertMsg("请选择文件", "warning");
				}
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#import_dialog").dialog("close");

				resetValue("#select_file");

				form.delete("file");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_filter + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	let datagrid = $("#member_list").datagrid({
		url: "/gang_member/selectByPage",
		height: height_table,
		fitColumns: true,
		pagination: true,
		remoteFilter: true,
		clientPaging: false,
		onHeaderContextMenu: function(e) {
			e.preventDefault();

			if (!columnMenu){
				createColumnMenu("#member_list");
			}
			columnMenu.menu("show", {
				left: e.pageX,
				top: e.pageY
			});
		},
		onLoadSuccess: function () {
			let selector = "#member_list";
			let fields = getFields(selector);

			if (fields.length > 0) {
				let datagrid = $(selector);

				for (let i = 0; i < fields.length; i++) {
					let field = fields[i];

					// 隐藏未选中的字段
					if (!field.selected) {
						datagrid.datagrid("hideColumn", field.name);
					}
				}

				datagrid.datagrid("fitColumns");
			}
		},
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-upload",
			text: "导入",
			handler: function() {
				importHandler();
			}
		}, "-", {
			iconCls: "icon-download",
			text: "导出",
			handler: function() {
				exportHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "id", title: "角色ID", align: "center", width:100, sortable: true},
			{field: "roleId", title: "角色造型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/role_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#role_" + rowIndex).attr({
							"src": data.image,
							"title": data.name
						});
					}, error);

					return "<img height='" + size + "' id='role_" + rowIndex + "' />";
				}
			},
			{field: "name", title: "帮派成员", align: "center", width: 100},
			{field: "grade", title: "等级", align: "center", width: 100, sortable: true},
			{field: "schoolId", title: "门派", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/school/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#school_" + rowIndex).attr({
							"src": data.image,
							"title": data.name
						});
					}, error);
					
					return "<img height='" + size + "' id='school_" + rowIndex + "' />";
				}
			},
			{field: "jobId", title: "职务", align: "center", width: 100, sortable: true
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/gang_job/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#job_" + rowIndex).html(data.name);
					}, error);
					
					return "<div id='job_" + rowIndex + "'></div>";
				}
			},
			{field: "bzbg", title: "本周帮贡", align: "center", width: 100, sortable: true},
			{field: "xybg", title: "现有帮贡", align: "center", width: 100, sortable: true},
			{field: "lsbg", title: "历史帮贡", align: "center", width: 100, sortable: true}
		]]
	});

	datagrid.datagrid("enableFilter", [{
		field: "name",
		type: "textbox",
		operator: ["equal", "contains"]
	}, {
		field: "grade",
		type: "numberspinner",
		operator: ["equal", "less", "lessorequal", "greater", "greaterorequal"],
		options: {
			min: 1,
			max: 115
		}
	}, {
		field: "bzbg",
		type: "numberspinner",
		operator: ["equal", "less", "lessorequal", "greater", "greaterorequal"],
		options: {
			min: 0
		}
	}, {
		field: "xybg",
		type: "numberspinner",
		operator: ["equal", "less", "lessorequal", "greater", "greaterorequal"],
		options: {
			min: 0
		}
	}, {
		field: "lsbg",
		type: "numberspinner",
		operator: ["equal", "less", "lessorequal", "greater", "greaterorequal"],
		options: {
			min: 0
		}
	}, {
		field: "jobId",
		type: "combobox",
		options: {
			url: "/gang_job/selectAll",
			valueField: "id",
			textField: "name",
			panelHeight: "auto"
		},
		operator: ["equal", "notequal"]
	}, {
		field: "roleId",
		type: "combobox",
		options: {
			url: "/role_category/selectAll",
			valueField: "id",
			textField: "name",
			panelHeight: "auto",
			groupField: "phyle",
			groupFormatter: function(group) {
				ajaxGet("/phyle/selectById", {
					id: group
				}, function (resp) {
					let data = resp.data;

					$("#phyle_" + group).html(data.name);
				}, error);

				return "<span class='blue' id='phyle_" + group + "'></span>";
			},
			formatter: function(row) {
				return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
			}
		},
		operator: ["equal", "notequal"]
	}, {
		field: "gangId",
		type: "combobox",
		options: {
			url: "/gang/selectAll",
			valueField: "id",
			textField: "name",
			panelHeight: "auto"
		},
		operator: ["equal", "notequal"]
	}, {
		field: "schoolId",
		type: "combobox",
		options: {
			url: "/school/selectAll",
			valueField: "id",
			textField: "name",
			panelHeight: "auto",
			formatter: function(row) {
				return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
			}
		},
		operator: ["equal", "notequal"]
	}]);

});